package com.money.service.impl;

import com.money.entity.*;
import com.money.service.CbbUserService;
import com.money.service.PayService;
import com.money.service.RapIdRearendService;
import com.money.service.UndoLogService;
import com.money.util.OrderIdUtil;
import io.seata.core.context.RootContext;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * 采用 tcc模式
 */
@Service
public class PayServiceImpl implements PayService {

    @Autowired
    private CbbUserService userService;
    @Autowired
    private RapIdRearendService rearendService;


    @Autowired
    private UndoLogService undoLogService;

    //分布式的事务
//    @GlobalTransactional
    // 我们的订单处理
    public void payOrder(ArrayList<Integer> ctIds) {
        String xid = RootContext.getXID();
        undoLogService.saveLogXid(xid);
        for (Integer ctId : ctIds) {
            //首先获取当前需要支付的购物车
            CbbShoppingCartEntity cartEntity = rearendService.info(ctId);
            //获取我们当前需要支付的金额 和我们的用户id
            Integer scUrId = cartEntity.getScUrId();
            //获取我们用户当前的金额
            CbbUserEntity userId = userService.getById(scUrId);
            //在获取我们当前商品的金额
            double count = cartEntity.getScPriceCount();
            //算出我们减去的值
            double yu = userId.getUrWallet() - count;
            userId.setUrWallet(yu);
            //保存修改信息后的用户
            userService.updateById(userId);

            //创建订单并且删除我们的此购物车的商品
            Long scSpId = cartEntity.getScSpId();
            CddShopEntity shopEntity = rearendService.infoId(scSpId);
            //创建订单
            CbbOrderEntity orderEntity = new CbbOrderEntity(OrderIdUtil.createId(), cartEntity.getScCount().toString(), shopEntity.getSpName()
                    , shopEntity.getSpName(), cartEntity.getScCount(), cartEntity.getScPriceCount()
                    , 1, cartEntity.getScUrId(), userId.getUrId(), "支付宝", shopEntity.getSpId());
            //保存好我们的订单
            boolean b = rearendService.saveOrder(orderEntity);
            System.out.println(b);
        }
        //最后只需要把我们的当前这个购物车的商品清理掉就行了
        Integer[] ids = new Integer[ctIds.size()];
        for (int i = 0; i < ctIds.size(); i++) {
            ids[i] = ctIds.get(i);
        }
        rearendService.delete(ids);
        //执行到证明程序没有出差然我们的当前的事务为正常的
        undoLogService.updateXid(xid);
    }

    @Override
    public boolean confirmOrder(BusinessActionContext businessActionContext) {
        String xid = businessActionContext.getXid();
        UndoLog undoLog = undoLogService.findXid(xid);
        Integer status = undoLog.getLog_status();
        return status == 0;
    }

    @Override
    public boolean cancelOrder(BusinessActionContext businessActionContext) {
        String xid = businessActionContext.getXid();
        UndoLog undoLog = undoLogService.findXid(xid);
        Integer status = undoLog.getLog_status();
        return status == 1;
    }
}
